package cn.com.pc.content.controller;

import cn.com.pc.content.domain.log.FrontendLog;
import cn.com.pc.content.domain.log.OperationLog;
import cn.com.pc.content.dto.FrontendLogForm;
import cn.com.pc.content.dto.OperationLogForm;
import cn.com.pc.content.dto.PageResult;
import cn.com.pc.content.dto.Sort;
import cn.com.pc.content.service.LogService;
import cn.com.pc.content.util.IpUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 后台日志管理
 */
@RestController
@RequestMapping("/logs")
public class LogController {

    @Autowired
    LogService logService;

    /**
     * 查询后台操作日志
     * @param operationLogForm
     * @param sort
     * @return
     */
    @GetMapping("/operation")
    public PageResult<?> listOperationlogsByForm(OperationLogForm operationLogForm, Sort sort) {
        return logService.listOperationLogsByForm(operationLogForm, sort);
    }

    /**
     * 保存前端报错日志
     * @param frontendLog
     * @param request
     * @return
     */
    @PostMapping("/frontend")
    public String saveFrontendLog(@RequestBody FrontendLog frontendLog, HttpServletRequest request) {
        frontendLog.setIp(IpUtils.getIp(request));
        frontendLog.setLogAt(LocalDateTime.now());
        return logService.saveFrontendLog(frontendLog);
    }

    /**
     * 查询前端报错日志
     * @param frontendLogForm
     * @param sort
     * @return
     */
    @GetMapping("/frontend")
    public PageResult<?> listFrontendLogsByForm(FrontendLogForm frontendLogForm, Sort sort) {
        return logService.listFrontendLogsByForm(frontendLogForm, sort);
    }
}
